//
//  PicTableViewCell.m
//  miaopai
//
//  Created by 杜昕宇 on 15/11/20.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "PicTableViewCell.h"
#import "CoreData.h"
#import "MacroDefinition.h"
#import "UIImageView+WebCache.h"

@interface PicTableViewCell()

@property (nonatomic, retain) UILabel *timeLabel;// 时间

@property (nonatomic, retain) UIView *lineView; // 时间下面的线

@property (nonatomic, retain) UILabel *detaiLabel; // 时间下面的 label;

@property (nonatomic, retain) UIButton *backView; // 视屏以及 gif 的 view

@property (nonatomic, retain) UIButton *likedButton; // 收藏

@property (nonatomic, retain) UIButton *shareButton; // 分享

@property (nonatomic, retain) UIImageView *backImage; // 图片

@property (nonatomic, copy) myBlock myBlock;

@property (nonatomic, copy) shareBlock shareblock;
@end

@implementation PicTableViewCell

- (void)dealloc
{
    Block_release(_shareblock);
    Block_release(_myBlock);
    [_backImage release];
    [_lineView release];
    [_likedButton release];
    [_timeLabel release];
    [_detaiLabel release];
    [_shareButton release];
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

//        创建视图
        [self creatViews];
    }
    return self;
}

- (void)creatViews{

    /******************* 时间 label ********************/
    self.timeLabel = [[UILabel alloc] init];
    //    _timeLabel.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:_timeLabel];
    [_timeLabel release];

    /********************线的 view ***********************/
    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_lineView];
    [_lineView release];

    /********************* 详情的 label*******************/
    self.detaiLabel = [[UILabel alloc] init];
    //    _detaiLabel.backgroundColor = [UIColor cyanColor];
    _detaiLabel.numberOfLines = 0;
    [self.contentView addSubview:_detaiLabel];
    [_detaiLabel release];

    /******************** backView ************************/
    self.backView = [[UIButton alloc] init];
    _backView.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:_backView];
    [_backView release];


    self.backImage = [[UIImageView alloc] init];
    _backImage.backgroundColor = [UIColor whiteColor];
    _backImage.userInteractionEnabled = YES;
    [_backView addSubview:_backImage];
    [_backImage release];
    /**********************喜欢****************************/
    self.likedButton = [[UIButton alloc] init];
    //    _likedButton.backgroundColor = [UIColor cyanColor];
    [_likedButton addTarget:self action:@selector(classButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_likedButton setImage:[UIImage imageNamed:@"liked"] forState:UIControlStateNormal];
    [self.contentView addSubview:_likedButton];
    [_likedButton release];

    /************************共享***************************/
    self.shareButton = [[UIButton alloc] init];
    //    _shareButton.backgroundColor = [UIColor cyanColor];
    [_shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [_shareButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [self.contentView addSubview:_shareButton];
    [_shareButton release];
}

- (void)shareAction:(UIButton *)button {
    self.shareblock();
}

- (void)shareButtonClicked:(shareBlock)block{
    self.shareblock = block;
}

#pragma mark - 取消收藏
- (void)classButtonAction:(UIButton *)button {
    self.myBlock();
}
- (void)classButtonClicked:(myBlock)block{
    self.myBlock = block;
}

#pragma mark - set方法
- (void)setPicture:(CollectionPicture *)picture{
    if (picture != _picture) {
        [_picture release];
        _picture = [picture retain];
        _timeLabel.text = picture.created_at;
        _detaiLabel.text = picture.text;
    }
}

#pragma mark - 自适应
- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat vWidth = SCREEN_WIDTH - 30;
    CGFloat vHeight = SCREEN_HEIGHT / 20;
    CGFloat imageHeight = SCREEN_HEIGHT / 30;
    _timeLabel.frame = CGRectMake(10, 0, vWidth , vHeight);
    _lineView.frame = CGRectMake(0, _timeLabel.Height + _timeLabel.Y, vWidth + 10, 1);
#warning 自适应高度
    _detaiLabel.frame = CGRectMake(_timeLabel.X, _timeLabel.Y + _timeLabel.Height + 5, vWidth, [[self class] heightOfText:_picture.text]);

    BOOL Screen_width = [_picture.width floatValue] < (SCREEN_WIDTH - 20) ;
    if (Screen_width) {
        _backView.frame = CGRectMake(0, _detaiLabel.Y + _detaiLabel.Height + 5, [_picture.width floatValue], [_picture.height floatValue]);
        [_backView setCenter:CGPointMake(vWidth / 2, _backView.Y + [_picture.height floatValue] / 2)];
    } else {
        CGFloat width = [_picture.width floatValue] / SCREEN_WIDTH;
        CGFloat height = [_picture.height floatValue] / width;
        _backView.frame = CGRectMake(0, _detaiLabel.Y + _detaiLabel.Height , SCREEN_WIDTH - 20, height);
    }
    _likedButton.frame = CGRectMake(_detaiLabel.X, _backView.Y + _backView.Height + 5, imageHeight, imageHeight);
    _backImage.frame = CGRectMake(0, 0, _backView.Width, _backView.Height);
    [_backImage sd_setImageWithURL:[NSURL URLWithString:_picture.original]];
    _shareButton.frame = CGRectMake( vWidth - 10 - imageHeight, _backView.Y + _backView.Height + 5,  imageHeight, imageHeight);
}

#pragma mark - 自适应高度 -- label
+ (CGFloat)heightOfText:(NSString *)text{

    CGSize size = CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect label = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return label.size.height;
}

#pragma mark - 类方法
+ (PicTableViewCell *)PicTableView:(UITableView *)tableView{
    static NSString *cellIdentify = @"picTableViewCell";
    PicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[PicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    return cell;

}

@end
